package com.hgd.security.evp.biz.rest;

import com.alibaba.fastjson.JSONObject;
import com.hgd.security.evp.biz.biz.EResourceAuthorityBiz;
import com.hgd.security.evp.biz.entity.EResourceAuthority;
import com.hgd.security.evp.common.util.ObjectResultResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("eResourceAuthority")
@Log4j
public class EResourceAuthorityController {

    @Autowired
    private EResourceAuthorityBiz iEResourceAuthorityBiz;

    @PostMapping("/authorityMenu")
    @ApiOperation("向组中添加菜单权限")
    public ObjectResultResponse<Void> addAuthorityMenu(
            @RequestParam("authorityId") @ApiParam(value = "组ID，即向哪个组里添加权限") String authorityId,
            @RequestParam("resourceId") @ApiParam(value = "菜单ID") String menuId
    ) {
    	log.info("向组中添加菜单权限--> /eResourceAuthority/authorityMenu");
        return iEResourceAuthorityBiz.addAuthorityMenu(authorityId,menuId);
    }

    @PostMapping("/authorityButton")
    @ApiOperation("向组中添加按钮权限")
    public ObjectResultResponse<Void> addAuthorityButton(
            @RequestParam("authorityId") @ApiParam(value = "组ID，即向哪个组里添加权限") String authorityId,
            @RequestParam("resourceId") @ApiParam(value = "菜单ID") String elementId
    ) {
        return iEResourceAuthorityBiz.addAuthorityButton(authorityId,elementId);
    }

    @PutMapping("/authority")
    @ApiOperation("向组中添加按钮权限")
    public ObjectResultResponse<Void> updateAuthority(
            @RequestBody @ApiParam("待更新对象实例") EResourceAuthority eResourceAuthority,
            BindingResult bindingResult
            ) {
        ObjectResultResponse<Void> resultResponse=new ObjectResultResponse<>();

        if (bindingResult.hasErrors()) {
            resultResponse.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
            resultResponse.setStatus(400);
            return resultResponse;
        }

        return iEResourceAuthorityBiz.updateAuthority(eResourceAuthority);
    }

    @DeleteMapping("/authorityMenu")
    @ApiOperation("删除组中的菜单权限,支持批量删除")
    public ObjectResultResponse<Void> delAuthorityMenu(
            @RequestParam("ids") @ApiParam(value = "待删除菜单权限ID，多个ID之间用逗号隔开") String ids
    ) {
        return iEResourceAuthorityBiz.delAuthorityMenu(ids);
    }

    @DeleteMapping("/authorityButton")
    @ApiOperation("删除组中的按钮权限,支持批量删除")
    public ObjectResultResponse<Void> delAuthorityButton(
            @RequestParam("ids") @ApiParam(value = "待删除按钮权限ID，多个ID之间用逗号隔开") String ids
    ) {
        return iEResourceAuthorityBiz.delAuthorityButton(ids);
    }
    
    @GetMapping("/authority")
    @ApiOperation("获取与用户关联的菜单及按钮")
    public JSONObject getAuthorityForUser() {
    	return this.getAuthorityForUser();
    }
}